package it.uniroma3.dia.reader;

import it.uniroma3.dia.db.DBDatasource;
import it.uniroma3.dia.db.DBQueryConstant;
import it.uniroma3.dia.models.RatingMovie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DBRatingMovieReader implements IRatingMovieRW {
	
	private int numberOfMovies;
	
	public DBRatingMovieReader(int numberOfMovies){
		this.numberOfMovies = numberOfMovies;
	}

	public List<RatingMovie> readRatingMovies() throws Exception{
		
		Connection connection = new DBDatasource().getConnection();

		PreparedStatement statement = connection.prepareStatement(DBQueryConstant.RANDOM_USERS_MOVIE);
		statement.setInt(1, numberOfMovies);
		ResultSet result = statement.executeQuery();
		
		List<RatingMovie> ratingMovies = new ArrayList<RatingMovie>();
		
		while(result.next()){
			int userID = result.getInt(1);
			int movieID = result.getInt(2);
			long timestamp = result.getLong(4);
			RatingMovie ratingMovie = new RatingMovie(userID, movieID, timestamp);
			ratingMovies.add(ratingMovie);
		}
		
		statement.close();
		connection.close();
		
		return ratingMovies;
	}

	@Override
	public void writeRatingMoviesPrediction(List<RatingMovie> ratingMovies)
			throws Exception {
		
		throw new Exception("write function unsupported for class "+this.getClass().getName());
	}
}
